/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs;

import org.digitalsoul.switchplugin.prefs.util.HelpTexts;
import org.digitalsoul.switchplugin.prefs.util.ItemListConfigurator;
import org.digitalsoul.switchplugin.prefs.util.UITools;
import org.digitalsoul.switchplugin.prefs.wizards.NewItemWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;

public class ItemsUI {

    private ItemListConfigurator configurator;
    private List itemsList;
    private Button removeButton;
    private Composite mainPanel;
    private Button newButton;

    public void build(Composite mainPanel, final ItemListConfigurator configurator) {
        this.mainPanel = mainPanel;
        this.configurator = configurator;
        
        // LABEL
        Label label = UITools.addLabel(mainPanel, "Switch Items");
        label.setLayoutData(UITools.getTwoSpanGridData());
        
        // HELP
        Label helpLabel = UITools.addHelpLabel(mainPanel, HelpTexts.getSwitchItemExplanation());
        helpLabel.setLayoutData(UITools.getTwoSpanGridData());
        
        // LIST
        itemsList = new List(mainPanel, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.minimumHeight = 80;
        data.grabExcessHorizontalSpace = true;
        data.grabExcessVerticalSpace = true;
        itemsList.setLayoutData(data);
        itemsList.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event arg0) {
                updateConfiguratorWithSelection();
            }
        });
        
        // BUTTONS
        Composite buttonPanel = new Composite(mainPanel, SWT.NONE);
        buttonPanel.setLayout(new GridLayout());
        data = new GridData();
        data.verticalAlignment = GridData.BEGINNING;
        buttonPanel.setLayoutData(data);
        
        // NEW...
        newButton = UITools.addButton(buttonPanel, "New...");    
        newButton.addListener(SWT.Selection, new NewButtonListener(this));
        
        // REMOVE
        removeButton = UITools.addButton(buttonPanel, "Remove");    
        removeButton.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                int index = itemsList.getSelectionIndex();
                configurator.removeItem(index);
                updateItems();
                UITools.selectListAfterRemove(itemsList, index);
            }
        });
        updateItems();
    }

    public void updateConfiguratorWithSelection() {
        if (itemsList.getSelectionIndex() >= 0) {
            configurator.selectItemByName(itemsList.getItem(itemsList.getSelectionIndex()));
        }
    }

    private void updateItems() {
        itemsList.removeAll();
        String[] itemNames = configurator.getItemNames();
        boolean hasItems = itemNames.length > 0;
        itemsList.setItems(itemNames);
        itemsList.setEnabled(hasItems);
        removeButton.setEnabled(hasItems);
        itemsList.update();
        mainPanel.layout();
        mainPanel.update();
    }

    public void addItem(String name) {
        configurator.addItem(name);
        updateItems();
        itemsList.select(itemsList.getItemCount() - 1);
        updateConfiguratorWithSelection();
    }

    private final class NewButtonListener implements Listener {

        private final ItemsUI itemsUI;

        public NewButtonListener(ItemsUI itemsUI) {
            this.itemsUI = itemsUI;
        }

        public void handleEvent(Event event) {
            UITools.openWizard(new NewItemWizard(itemsUI, configurator));
        }
    }
}
